package com.zy.explore.sys.domain.mybatis.entity;


import com.zy.explore.common.interfaces.frm.SortFieldFrm;
import com.zy.explore.sys.interfaces.frm.SysUserFrm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 系统用户表(表sys_user)ORM实体对象
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年06月13日 12:26:11
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "sys_user")
public class SysUser {

    /**
     * 系统用户ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    private Integer id;

    /**
     * 钉钉用户ID
     */
    private String dtUserId;

    /**
     * 姓名
     */
    private String name;

    /**
     * 手机号码
     */
    private String phone;

    /**
     * 住宅电话
     */
    private String telephone;

    /**
     * 联系地址
     */
    private String address;

    /**
     * 是否可用：0.禁用 1.可用
     */
    private Boolean enabled;

    /**
     * 在职状态：0.已离职 1.在职
     */
    private Boolean leaveStatus;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 备注
     */
    private String remark;

    /**
     * 离职日期
     */
    private Date leaveDate;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 用户角色集合
     */
    private List<SysRole> roles = new ArrayList<>();

    /**
     * 用户菜单集合
     */
    private List<SysMenu> menus = new ArrayList<>();

    /**
     * 获取用户权限集合
     *
     * @return {@link Collection<? extends GrantedAuthority>}
     */
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (SysRole role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    /**
     * Frm转Example条件查询对象
     *
     * @param frm 查询参数对象
     * @return 返回Example对象
     */
    public static Example getExample(SysUserFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (frm.getId() != null) {
            criteria.andEqualTo("id", frm.getId());
        }
        if (!StringUtils.isEmpty(frm.getName())) {
            criteria.andLike("name", "%" + frm.getName() + "%");
        }
        if (!StringUtils.isEmpty(frm.getPhone())) {
            criteria.andLike("phone", "%" + frm.getPhone() + "%");
        }
        if (!StringUtils.isEmpty(frm.getTelephone())) {
            criteria.andLike("telephone", "%" + frm.getTelephone() + "%");
        }
        if (!StringUtils.isEmpty(frm.getAddress())) {
            criteria.andLike("address", "%" + frm.getAddress() + "%");
        }
        if (frm.getEnabled() != null) {
            criteria.andEqualTo("enabled", frm.getEnabled());
        }
        if (!StringUtils.isEmpty(frm.getUsername())) {
            criteria.andLike("username", "%" + frm.getUsername() + "%");
        }
        if (!StringUtils.isEmpty(frm.getPassword())) {
            criteria.andLike("password", "%" + frm.getPassword() + "%");
        }
        if (!StringUtils.isEmpty(frm.getAvatar())) {
            criteria.andLike("avatar", "%" + frm.getAvatar() + "%");
        }
        if (!StringUtils.isEmpty(frm.getRemark())) {
            criteria.andLike("remark", "%" + frm.getRemark() + "%");
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("createTime", frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("updateTime", frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", frm.getUpdateTimeEnd());
        }
        // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按主键降序排序
            example.orderBy("id").desc();
        }
        return example;
    }
}